package com.datashare.dataentryods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.datashare.dataentryods.domain.IYjzfbl;
import com.datashare.dataentryods.mapper.yjzf.IYjzfblMapper;
import com.datashare.dataentryods.service.IYjzfblService;
import com.ruoyi.common.core.service.BaseServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import java.util.Arrays;
import java.util.List;

/**
 * 多级菜单示例信息Service业务层处理
 * 
 * @author lhj
 * @date 2022-02-11
 */
@Service("yjzfblServiceImpl")
public class iYjzfblServiceImpl extends BaseServiceImpl<IYjzfblMapper, IYjzfbl> implements IYjzfblService
{
    @Autowired
    private IYjzfblMapper iYjzfblMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询多级菜单示例信息
     * 
     * @param yjzfblId 多级菜单示例信息主键
     * @return 多级菜单示例信息
     */
    @Override
    public IYjzfbl selectYjzfblByYjzfblId(Long yjzfblId)
    {
        return iYjzfblMapper.selectYjzfblByYjzfblId(yjzfblId);
    }

    /**
     * 查询多级菜单示例信息列表
     * 
     * @param iYjzfbl 多级菜单示例信息
     * @return 多级菜单示例信息
     */
    @Override
    public List<IYjzfbl> selectYjzfblList(IYjzfbl iYjzfbl)
    {
        return iYjzfblMapper.selectYjzfblList(iYjzfbl);
    }

    /**
     * 新增多级菜单示例信息
     * 
     * @param iYjzfbl 多级菜单示例信息
     * @return 结果
     */
    @Override
    public int insertYjzfbl(IYjzfbl iYjzfbl)
    {
        iYjzfbl.setCreateBy(SecurityUtils.getUsername());
        iYjzfbl.setCreateTime(DateUtils.getNowDate());
        iYjzfbl.setSjc(DateUtils.getNowDate());
        return iYjzfblMapper.insert(iYjzfbl);
    }

    /**
     * 修改多级菜单示例信息
     * 
     * @param iYjzfbl 多级菜单示例信息
     * @return 结果
     */
    @Override
    public int updateYjzfbl(IYjzfbl iYjzfbl)
    {
        iYjzfbl.setUpdateBy(SecurityUtils.getUsername());
        iYjzfbl.setUpdateTime(DateUtils.getNowDate());
        iYjzfbl.setSjc(DateUtils.getNowDate());
        return iYjzfblMapper.updateById(iYjzfbl);
    }

    /**
     * 批量删除多级菜单示例信息
     * 
     * @param yjzfblIds 需要删除的多级菜单示例信息主键
     * @return 结果
     */
    @Override
    public int deleteYjzfblByYjzfblIds(Long[] yjzfblIds)
    {
        List<Long> idList = Arrays.asList(yjzfblIds);
        return iYjzfblMapper.deleteBatchIds(idList);
    }

    /**
     * 删除多级菜单示例信息信息
     * 
     * @param yjzfblId 多级菜单示例信息主键
     * @return 结果
     */
    @Override
    public int deleteYjzfblByYjzfblId(Long yjzfblId)
    {
        return iYjzfblMapper.deleteById(yjzfblId);
    }
    
    /**
     * 导入多级菜单示例信息数据
     * 注意提示信息中的yjzfbl.getCreateBy()，应该替换成有效的字段
     * @param iYjzfblList 多级菜单示例信息数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importYjzfbl(List<IYjzfbl> iYjzfblList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(iYjzfblList) || iYjzfblList.size() == 0)
        {
            throw new ServiceException("导入多级菜单示例信息数据不能为空,请检查导入的Excel文件！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //String password = configService.selectConfigByKey("sys.user.initPassword");
        for (IYjzfbl iYjzfbl : iYjzfblList)
        {
            try {
                BeanValidators.validateWithException(validator, iYjzfbl);
            } catch (ConstraintViolationException e){
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、导入多级菜单示例信息: "+ iYjzfbl.getDqmc() + " " + e.getMessage().substring(e.getMessage().indexOf(":")+1));
                continue;
            }
            QueryWrapper<IYjzfbl> wrapper=new QueryWrapper<>();
            wrapper.eq(StringUtils.isNotBlank(iYjzfbl.getDqdm()), "dqdm", iYjzfbl.getDqdm());
            wrapper.eq(StringUtils.isNotBlank(iYjzfbl.getDqmc()), "dqmc", iYjzfbl.getDqmc());
            wrapper.eq(null!= iYjzfbl.getZfbl(), "zfbl", iYjzfbl.getZfbl());
            wrapper.eq(null!= iYjzfbl.getKsrq(), "ksrq", iYjzfbl.getKsrq());
            wrapper.eq(null!= iYjzfbl.getJsrq(), "jsrq", iYjzfbl.getJsrq());

            try
            {
                // 验证是否存在这条数据
                IYjzfbl u = iYjzfblMapper.selectOne(wrapper);
                if (StringUtils.isNull(u))
                {
                    iYjzfbl.setCreateBy(operName);
                    iYjzfbl.setCreateTime(DateUtils.getNowDate());
                    iYjzfbl.setSjc(DateUtils.getNowDate());
                    if (null== iYjzfbl.getJsrq()) iYjzfbl.setJsrq(DateUtils.parseDate("2999-12-31"));
                    iYjzfblMapper.insert(iYjzfbl);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、导入多级菜单示例信息 " + iYjzfbl.getDqmc() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    iYjzfbl.setUpdateBy(operName);
                    iYjzfbl.setUpdateTime(DateUtils.getNowDate());
                    iYjzfbl.setSjc(DateUtils.getNowDate());
                    iYjzfblMapper.update(iYjzfbl, wrapper);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、导入多级菜单示例信息 " + iYjzfbl.getDqmc() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、导入多级菜单示例信息 " + iYjzfbl.getDqmc() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、导入多级菜单示例信息 " + iYjzfbl.getDqmc() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
